$(function () {

    let id = window.location.search.split('=')[1]
    console.log(id);
    // index_article: baseURL + '/index/article',//文章详细内容 
    $.ajax({
        type: 'get',
        url: BigNew.index_article,
        data: {
            id
        },
        success(backData) {
            console.log(backData);
            let v = backData.data
            if (backData.code == 200) {
                $('.breadcrumb').children('a').eq(1).text(v.category).end().attr('href', './list.html?id=' + v.categoryId);
                $('.article_title').text(v.title);
                $('.article_info').text(`${v.author} 发布于 ${v.date}   分类: ${v.category}   阅读:
                (${v.read})   评论: (${v.comments})`);
                $('.article_con').html(v.content);
                // 文章
                if (v.prev == null) {
                    $('.article_links>a:eq(0)').text('没有上一篇')
                } else {
                    $('.article_links>a:eq(0)').text(v.prev.title);
                    $('.article_links>a:eq(0)').attr('href', './article.html?id=' + v.prev.id);
                }
                if (v.next == null) {
                    $('.article_links>a:eq(1)').text('没有下一篇')
                } else {
                    $('.article_links >a:eq(1)').text(v.next.title);
                    $('.article_links >a:eq(1)').attr('href', './article.html?id=' + v.next.id);
                }



            };
        },



    });

    //最新评论
    $.ajax({
        type: 'get',
        url: BigNew.index_latest_comment,
        success(backData) {
            // console.log(backData);
            if (backData.code == 200) {
                let resHtml = template('commentTem', backData);
                $('.comment_list').html(resHtml)
            };
        },
    });

    //文章热门排行
    $.ajax({
        type: 'get',
        url: BigNew.index_rank,
        success(backData) {
            if (backData.code == 200) {
                for (let i = 0; i < backData.data.length; i++) {
                    $('.content_list li').eq(i).children('a').text(backData.data[i].title);
                    $('.content_list li').eq(i).children('a').attr('href', './article.html?id=' + backData.data[i].id);
                };
            };

        },
    });

    //焦点关注
    $.ajax({
        type: 'get',
        url: BigNew.index_attention,
        success(backData) {
            // console.log(backData);
            if (backData.code == 200) {
                for (let i = 0; i < backData.data.length; i++) {
                    $('.guanzhu_list li').eq(i).children('a').text(backData.data[i].intro);
                    $('.guanzhu_list li').eq(i).children('a').attr('href', './article.html?id=' + backData.data[i].id);
                }

            };
        },
    });

    //文章类型 
    $.ajax({
        type: 'get',
        url: BigNew.index_category,
        success(backData) {
            if (backData.code == 200) {
                let resHtml = template('categoryTem', backData)
                $('.left_menu ').html(resHtml)
                $('.level_two ').html('<li class="up"></li>' + resHtml);

            };
        },
    });

    //文章下面的评论
    $.ajax({
        type: 'get',
        url: BigNew.index_get_comment,
        data: {
            articleId: id
        },
        success(backData) {
            console.log(backData);
            if (backData.code == 200) {
                let resHtml = template('get_comment', backData)
                $('.comment_list_con').html(resHtml);
                $('.comment_count').text(backData.data.length + '条评论')
            }
        }
    });

    // 发表评论
    $('.comment_sub').click(function (e) {
        e.preventDefault()
        let author = $('.comment_name').val().trim();
        let content = $('.comment_input').val().trim();
        if (author == '' || content == '') {
            alert('请输入内容');
            return
        }
        $.ajax({
            type: 'post',
            url: BigNew.index_post_comment,
            data: {
                author,
                content,
                articleId: id
            },
            success(backData) {
                console.log(backData);
                if (backData.code == 201) {
                    alert('发布成功，等待管理员审核');
                    $('.comment_name').val('')
                    $('.comment_input').val('')
                }
            }

        });
    });

});
